<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class FrontendBase {
	var $content_wrap = array();
	var $context_wrap = array();
	var $data = '';
	var $tpl = '';
	var $permission_tpl_file = 'pagelayout/pagelayout_nopermission.tpl';
	
	//constructor
	function FrontendBase() {
		global $tpl;
		global $request_type, $request_link, $request_link_class, $request_link_subclass, $request_link_param, $request_link_current;
		global $request_param;
		global $action, $type, $operation;
		global $page;
		global $limit_start, $limit_length;
		global $SETTINGS;
		global $LANGUAGE;
		
		$this->logger = NULL;
		$this->tpl = $tpl;
		//
		$this->request_type = $request_type;
		$this->request_link = $request_link;
		$this->request_link_class = $request_link_class;
		$this->request_link_subclass = $request_link_subclass;
		$this->request_link_param = $request_link_param;
		$this->request_link_current = $request_link_current;
		$this->request_param = $request_param;
		$this->_request = $_REQUEST;
		$this->_get = $_GET;
		$this->_post = $_POST;
		//
		$this->action = $action;
		$this->type = $type;
		$this->operation = $operation;
		$this->limit_page = $page;
		$this->limit_start = $limit_start;
		$this->limit_length = $limit_length;
		//
		$this->SETTINGS = $SETTINGS;
		$this->language_wrap = $LANGUAGE;
		$this->style_dir_path = CONFIG_URL_HTTP . CONFIG_DIR_STYLES . CONFIG_STYLE . '/';
		$this->css_dir_path = CONFIG_URL_HTTP . CONFIG_DIR_STYLES . CONFIG_STYLE . '/css/';
		$this->js_dir_path = CONFIG_URL_HTTP . CONFIG_DIR_JS;
		//
		$this->doEvents('init');
	}
	
	function addBeforeProcess($param) {
		$this->triggers['BeforeProcess'][] = $param;
	}
	
	function addBeforeInitContext($param) {
		$this->triggers['BeforeInitContext'][] = $param;
	}
	
	function addAfterInitContext($param) {
		$this->triggers['AfterInitContext'][] = $param;
	}
	
	function addPostProcess($param) {
		$this->triggers['PostProcess'][] = $param;
	}
	
	function addAfterProcess($param) {
		$this->triggers['AfterProcess'][] = $param;
	}
	
	function addTrigger($param) {
		return false;
	}
	
	function fetch($tpl_file, $cache_id = '') {
		$this->doEvents('beforProcess');
		if( $this->context_inited == false ) {
			$this->doEvents('beforeInitContext');
			$this->initContext();
			$this->appendCssJsFiles();
			$this->appendContext();
			$this->doEvents('afterInitContext');
		}
		
		$this->doEvents('injectProcess');
		
		//ASSIGN
		$this->tpl->assign("CONTENT", $this->content_wrap);
		$this->tpl->assign("CONTEXT", $this->context_wrap);
		$this->tpl->assign("LANGUAGE", $this->language_wrap);
		$this->tpl->assign("SETTINGS", $this->SETTINGS);
		
		//CONTENT
		$cache_id = $this->tpl->cache_id;
		if( $tpl_file == '' ) {
			$tpl_file = $this->tpl->tpl_file;
		}
		$this->content = $this->tpl->fetch($tpl_file, $cache_id);
		$this->doEvents('afterProcess');
		
		return $this->content;
	}
	
	function display($tpl_file, $cache_id = '') {
		return $this->fetch($tpl_file, $cache_id);
	}
	
	function output($tpl_file, $cache_id = '') {
		echo $this->fetch($tpl_file, $cache_id);
	}
	
	function close($tpl_file = '', $cache_id = '') {
		if( $tpl_file == '' ) {
			$tpl_file = 'pagelayout/pagelayout_close.tpl';
		}
		echo $this->display($tpl_file, $cache_id);
	}
	
	//
	//Event
	function doEvents($method, $params = '') {
		$method = 'on' . ucfirst($method);
		if( method_exists($this, $method) ) {
			$this->$method($params);
		}
	}
	
	//Event handler
	function onInit($params = '') {
		$this->doEvents('beforeInitContext');
		$this->initContext();
		$this->appendCssJsFiles();
		$this->appendContext();
		$this->doEvents('afterInitContext');
	}
	
	function onBeforProcess($params = '') {
		
	}
	
	function onBeforeInitContext($params = '') {
		
	}
	
	function onAfterInitContext($params = '') {
		if( is_array($this->triggers['AfterInitContext']) ) {
			foreach($this->triggers['AfterInitContext'] as $trigger) {
				if( is_string($trigger) && method_exists($this, $trigger) ) {
					$this->$trigger();
				}
			}
		}
	}
	
	function onInjectProcess($params = '') {
		$this->inject();
	}
	
	function onAfterProcess($params = '') {
		
	}
	
	//CONTENT
	function getContent() {
		return $this->content_wrap;
	}
	
	function addContent($key, $value) {
		if( isset($this->content_wrap[$key]) ) {
			$this->content_wrap[$key] = $value;
		} else {
			$this->content_wrap[$key] = $value;
		}
	}
	
	function setContent($key, $value) {
		if( isset($this->content_wrap[$key]) ) {
			$this->content_wrap[$key] = $value;
		} else {
			$this->content_wrap[$key] = $value;
		}
	}
	
	function appendContent($key, $value) {
		if( isset($this->content_wrap[$key]) ) {
			$this->content_wrap[$key] = $value;
		} else {
			$this->content_wrap[$key] = $value;
		}
	}
	
	//CSS
	function getCssFiles($type = '') {
		$cssfilesa = array();
		$cssfilesb = $this->getCssFilesBase();
		//$cssfilesa = $this->getCssFilesA($type);
		
		$cssfiles = array_merge($cssfilesb, $cssfilesa);
		
		return $cssfiles;
	}
	
	function getCssFilesBase() {
		$cssfiles = array(
				$this->css_dir_path . 'g.css',
				$this->css_dir_path . 'l.css',
				$this->css_dir_path . 'c.css',
					);
		
		return $cssfiles;
	}
	
	function getCssFilesSpecial() {
		$themecss = '';
		if( isset($_SESSION['styletheme']) ) {
			$themecss = CONFIG_URL_HTTP . CONFIG_DIR_STYLES . $_SESSION['styletheme']['path'] . '/css/' . $_SESSION['styletheme']['link'] . '.css';
		} else {
			$themecss = $this->css_dir_path . 'default.css';
		}
		if( isset($_SESSION['stylestatus']) && $_SESSION['stylestatus'] > 0 && isset($_SESSION['stylesamplecssfile']) ) {
			if( substr($_SESSION['stylesamplecssfile'], 0, strlen('blog#')) == 'blog#' ) {
				$stylesamplecss = CONFIG_URL_HTTP . 'styles/blog/' . substr($_SESSION['stylesamplecssfile'], strlen('blog#')) . '/css/stylesheet.css';
			} else {
				$stylesamplecss = CONFIG_URL_HTTP . 'style/' . $_SESSION['stylesamplecssfile'];
			}
		} else {
			$stylesamplecss = CONFIG_URL_HTTP . 'style/style_diy.css';
		}
		
		$cssfiles_special = array(
						array('css' => $themecss, 'id' => '_template_link'),
						array('css' => $stylesamplecss, 'id' => '_template_sample'),
							);
		
		return $cssfiles_special;
	}
	
	function getCssFilesA($type = '') {
		if( $type == 'admin' ) {
			$context_wrap['CSSFILES'][] = $this->css_dir_path . 'a.css';
			$context_wrap['CSSFILES_SPECIAL'] = array();
		} elseif( $type == 'company' ) {
			$context_wrap['CSSFILES'][] = $this->css_dir_path . 'company.css';
			$context_wrap['CSSFILES_SPECIAL'] = array();
			$context_wrap['CSSFILES_SPECIAL'][] = array('css' => $themecss, 'id' => '_template_link');
		} elseif( $type == 'group' ) {
			$context_wrap['CSSFILES'][] = $this->css_dir_path . 'group.css';
			
			$context_wrap['JSFILES'][] = $this->js_dir_path . 'group.js';
		} elseif( $type == 'item' ) {
			$context_wrap['CSSFILES'][] = $this->css_dir_path . 'citem.css';
			
			$context_wrap['JSFILES'][] = $this->js_dir_path . 'citem.js';
		} else {
			$context_wrap['CSSFILES'][] = $this->css_dir_path . 'm.css';
			if( $type == 'home' || $type == '' || $type == 'index' ) {
				$context_wrap['CSSFILES'][] = $this->css_dir_path . 'h.css';
				//$cssfiles_special[] = array('css' => $this->css_dir_path . 'h.css', 'id' => '_home_link');
			}
		}
		
		return $cssfiles_special;
	}
	
	//JS
	function getJsFiles($type = '') {
		return $this->getJsFilesBase($type);
	}
	
	function getJsFilesBase($type = '') {
		if( $type == 'admin' ) {
			$jsfiles = array(
					$this->js_dir_path . 'common.js',
					$this->js_dir_path . 'base.js',
					$this->js_dir_path . 'ajax.js',
					//$this->js_dir_path . 'calendar.js',
					$this->js_dir_path . 'editor.js',
					//$this->js_dir_path . 'utils.js',
					//$this->js_dir_path . 'admin.js',
						);
		} else {
			$jsfiles = array(
					$this->js_dir_path . 'common.js',
					$this->js_dir_path . 'base.js',
					$this->js_dir_path . 'ajax.js',
					//$this->js_dir_path . 'calendar.js',
					//$this->js_dir_path . 'utils.js',
					//$this->js_dir_path . 'admin.js',
						);
		}
		
		return $jsfiles;
	}
	
	function getJsFilesA($type = '') {
		
	}
	
	function getJsEditor() {
		return CONFIG_URL_HTTP . 'editor/tinymce/jscripts/tiny_mce/tiny_mce_src.js';
	}
	
	//CSS/JS
	function appendCssJsFiles($type = '') {
		if( $type == 'admin' || $this->request_type == 'admin' ) {
			if( isset($this->SETTINGS['adminstyle']) && $this->SETTINGS['adminstyle'] != '' ) {
				$this->context_wrap['CSSFILES'][] = $this->css_dir_path . '' . $this->SETTINGS['adminstyle'] . '.css';
			}
			$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'a.css';
		} elseif( $type == 'member' || $this->request_type == 'member' ) {
			if( isset($this->SETTINGS['memberstyle']) && $this->SETTINGS['memberstyle'] != '' ) {
				$this->context_wrap['CSSFILES'][] = $this->css_dir_path . '' . $this->SETTINGS['memberstyle'] . '.css';
			}
			$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'member.css';
		} elseif( $type == 'company' ) {
			$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'company.css';
			$this->context_wrap['CSSFILES_SPECIAL'] = array();
			$this->context_wrap['CSSFILES_SPECIAL'][] = array('css' => $themecss, 'id' => '_template_link');
		} elseif( $type == 'group' ) {
			$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'group.css';
			
			$this->context_wrap['JSFILES'][] = $this->js_dir_path . 'group.js';
		} elseif( $type == 'item' ) {
			$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'citem.css';
			
			$this->context_wrap['JSFILES'][] = $this->js_dir_path . 'citem.js';
		} elseif( $this->request_type == 'home' || $this->request_type == '' || $this->request_type == 'index' ) {
			$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'h.css';
		} else {
			if( isset($this->SETTINGS['style']) && $this->SETTINGS['style'] != '' ) {
				$this->context_wrap['CSSFILES'][] = $this->css_dir_path . '' . $this->SETTINGS['style'] . '.css';
			} else {
				$this->context_wrap['CSSFILES'][] = $this->css_dir_path . 'm.css';
			}
		}
	}
	
	function addJsFile($js) {
		$this->context_wrap['JSFILES'][] = $this->js_dir_path . $js;
	}
	
	function addJsFileExt($js) {
		$this->context_wrap['JSFILES'][] = $js;
	}
	
	function addCssFile($css) {
		$this->context_wrap['CSSFILES'][] = $this->css_dir_path . $css;
	}
	
	function cleanJsFiles($js = '') {
		if( $js == '' ) {
			$js = array('base.js', 'ajax.js');
		}
		if( !is_array($js) ) {
			$js = array($js);
		}
		foreach($js as $k => $v) {
			$js[$k] = $this->js_dir_path . $v;
		}
		//
		foreach($this->context_wrap['JSFILES'] as $k => $v) {
			if( in_array($v, $js) ) {
				unset($this->context_wrap['JSFILES'][$k]);
			}
		}
		//
		sort($this->context_wrap['JSFILES']);
	}
	
	//MESSAGESTACK
	function getMessageStack() {
		global $messagestackObj;
		
		$ms_size = $messagestackObj->size('header');
		$ms_content = $messagestackObj->output('header');
		
		$messagestack = array(
					'size' => $ms_size,
					'content' => $ms_content,
							);
		
		return $messagestack;
	}
	
	//BREADCRUMB
	function getBreadcrumb() {
		global $bcObj;
		$breadcrumb = $bcObj->toString();
		
		return $breadcrumb;
	}
	
	//CONTEXT
	function initContext() {
		if( $this->context_inited == true ) {
			return true;
		}
		if( $this->SETTINGS['bs_company'] ) {
			$title = $this->SETTINGS['bs_company'];
		} elseif( defined('CONFIG_COMPANY_NAME') ) {
			$title = CONFIG_COMPANY_NAME;
		} else {
			$title = '';
		}
		if( $this->SETTINGS['bs_company'] ) {
			$cname = $this->SETTINGS['bs_company'];
		} elseif( defined('CONFIG_COMPANY_NAME') ) {
			$cname = CONFIG_COMPANY_NAME;
		} else {
			$cname = '';
		}
		
		$context_wrap = array(
				'URLBASE' => CONFIG_URL_HTTP,
				'URL' => CONFIG_URL_HTTP . SYS_RT_LANG_URI,
				'REQUEST_URI' => ((substr(SYS_RT_REQUEST_URI, 0, 1) == '/') ? substr(SYS_RT_REQUEST_URI, 1) : SYS_RT_REQUEST_URI),
				'URL_PREFIX' => CONFIG_URL_PREFIX,
				'URL_SUFFIX' => CONFIG_URL_SUFFIX,
				'STYLE' => CONFIG_STYLE,
				//
				'LANG' => SYS_RT_LANG,
				'LANG_URI' => SYS_RT_LANG_URI,
				//
				'TITLE' => $title,
				//
				'CSSFILES' => $this->getCssFiles($this->request_type),
				//'CSSFILES_SPECIAL' => $this->getCssFilesSpecial(),
				'JSFILES' => $this->getJsFiles($this->request_type),
				//
				'user' => array(
						'id' => $_SESSION['suser']->userid,
						'name' => $_SESSION['suser']->username,
						'type' => $_SESSION['suser']->usertype,
						'gid' => $_SESSION['suser']->usergid,
							),
				'user_login' => (isset($_SESSION['user_login']) ? $_SESSION['user_login'] : 'false'),
				'request' => array(
						'type' => $this->request_type,
						'link' => $this->request_link,
						'class' => $this->request_link_class,
						'subclass' => $this->request_link_subclass,
							),
				//'server' => $_SERVER,
				//'referer' => $_SERVER['HTTP_REFERER'],
				'param' => $_GET,
						);
		//
		$this->context_wrap = $context_wrap;
		$this->context_inited = true;
		
		return $context_wrap;
	}
	
	function appendContext() {
		$this->context_wrap['messagestack'] = $this->getMessageStack();
		$this->context_wrap['breadcrumb'] = $this->getBreadcrumb();
	}
	
	function getContext() {
		if( $this->context_inited == false ) {
			$this->initContext();
			$this->appendCssJsFiles();
			$this->appendContext();
		}
		
		return $this->context_wrap;
	}
	
	function addContext($key, $value) {
		if( isset($this->context_wrap[$key]) ) {
			$this->context_wrap[$key] = $value;
		} else {
			$this->context_wrap[$key] = $value;
		}
	}
	
	function setContext($key, $value) {
		if( isset($this->context_wrap[$key]) ) {
			$this->context_wrap[$key] = $value;
		} else {
			$this->context_wrap[$key] = $value;
		}
	}
	
	function setTitle($value) {
		$this->context_wrap['TITLE'] = $value;
	}
	
	function addTitle($value, $sep = ' ') {
		$this->context_wrap['TITLE'] = $value . $sep . $this->context_wrap['TITLE'];
	}
	
	function appendTitle($value) {
		$this->context_wrap['TITLE'] = $this->context_wrap['TITLE'] . $sep . $value;
	}
	
	//INJECT
	function inject() {
		global $injectObj;
		if( is_object($injectObj) ) {
			$content_data = $injectObj->get('func_get_tpl_pagelayout_content', 'content_wrap');
			if( is_array($content_data) ) {
				foreach($content_data as $k => $data) {
					$this->content_wrap = array_merge($this->content_wrap, $data);
				}
			}
			$context_data = $injectObj->get('func_get_tpl_pagelayout_content', 'context_wrap');
			if( is_array($context_data) ) {
				foreach($context_data as $k => $data) {
					$this->context_wrap = array_merge($this->context_wrap, $data);
				}
			}
		}
	}
	
	function addInject($key, $value) {
		global $injectObj;
		$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', $key, $value);
	}
	
	//
	function outputScript($content, $back = false) {
		header("Location: Content-type: text/xml; charset=utf8");
		echo $content;
		if( $back == true ) {
			echo $this->addBack();
		}
		exit();
	}
	
	function addBack() {
		return '<script type="text/javascript" language="javascript">history.go(-1);</script>';
	}
	
	function addAlert($string) {
		return '<script type="text/javascript" language="javascript">alert(\'' . $string . '\');</script>';
	}
	
	function redirect($url, $msg = '') {
		util_redirect(SYS_RT_LANG_URI . $url, $msg);
	}
	
	//
	function checkPermission($code) {
		global $userObj;
		if( $_SESSION['suser']->usergid > 100 ) {
			return true;
		}
		if( $userObj->checkPermission($code) == false ) {
			$this->outputPermission($code);
		}
	}
	
	function checkPermissionPage($code) {
		
	}
	
	function checkPermissionAction($code, $action = '') {
		global $userObj;
		
		//
		if( $_SESSION['suser']->usergid > 100 ) {
			return true;
		}
		if( $action != '' ) {
			$code = $code . '::update';
		} else {
			$code = $code . '::view';
		}
		if( $userObj->checkPermission($code) == false ) {
			if( $action != '' ) {
				global $action;
				$action = '';
				
				//
				global $injectObj;
				$vm = array('title' => 'Tip', 'message' => 'No Permission', 'show' => 1);
				$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
			}
			$this->outputPermission($code);
		}
	}
	
	function checkPermissionRequest($class, $subclass = '', $action = '') {
		//
		if( $_SESSION['suser']->usergid > 100 ) {
			return true;
		}
		//
		if( $class == '' || $class == 'index' ) {
			return true;
		} elseif( $class == 'static' ) {
			$class = 'page';
		}
		//
		if( $subclass == '' ) {
			$code = $class . '_admin';
		} else {
			//$code = $class . '_' . $subclass;
			$code = $class . '_admin';
		}
		$this->checkPermissionAction($code, $action);
	}
	
	function setNoPermission($code, $tpl_file = '', $cache_id = '') {
		$this->permission_code = $code;
		if( $tpl_file != '' ) {
			$this->permission_tpl_file = $tpl_file;
		}
		$this->nopermission = 1;
	}
	
	function outputPermission($code, $tpl_file = '', $cache_id = '') {
		$this->permission_code = $code;
		if( $tpl_file != '' ) {
			$this->permission_tpl_file = $tpl_file;
		}
		$this->nopermission = 1;
		echo $this->display($this->permission_tpl_file, $cache_id);
		exit();
		//$this->outputScript($this->addAlert('No Permission'), true);
	}
	
	//
	function &getObj($class, $filename = '') {
		if( $filename == '' ) {
			$filename = strtolower($class) . '.class.php';
		}
		if( file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $filename) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $filename);
		}
		if( class_exists($class) ) {
			$obj = new $class();
		} elseif( class_exists($class . 'Base') ) {
			$class = $class . 'Base';
			$obj = new $class();
		} else {
			$obj = NULL;
		}
		
		return $obj;
	}
	
	//lang
	function lang($key, $module = '') {
		if( empty($key) ) {
			return '';
		}
		global $LANGUAGE;
		if( $module == '' ) {
			return (isset($LANGUAGE[$module][$key]) ? $LANGUAGE[$module][$key] : $key);
		} else {
			return (isset($LANGUAGE[$key]) ? $LANGUAGE[$key] : $key);
		}
	}
	
	//
	function setting($key) {
		if( isset($this->SETTINGS[$key]) ) {
			return $this->SETTINGS[$key];
		}
		
		return NULL;
	}
	
	//cache
	function cache_id($tpl_file = '', $key = '') {
		
	}
	
	function is_cached() {
		
	}
	
	function clean_cache() {
		
	}
	
	//message
	function _message() {
		
	}
	
	function add_message() {
		
	}
	
	function clean_message() {
		
	}
}
//
?>